<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="queryParams" ref="queryFormRef" inline>
        <el-form-item label="公告标题" prop="noticeTitle">
          <el-input
            v-model="queryParams.noticeTitle"
            placeholder="请输入标题"
            clearable
          />
        </el-form-item>

        <el-form-item label="操作人员" prop="createBy">
          <el-input
            v-model="queryParams.createBy"
            placeholder="请输入操作人员"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="primary" icon="Plus" @click="handleEdit()"
          >新增</el-button
        >
      </template>

      <el-table v-loading="loading" :data="dataList" highlight-current-row>
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="60"
          :index="
            (index) => {
              return (
                1 + index + (queryParams.pageNum - 1) * queryParams.pageSize
              );
            }
          "
        />
        <el-table-column
          label="公告标题"
          align="center"
          prop="noticeTitle"
          width="300"
        />
        <el-table-column label="公告类型" align="center">
          <template #default="scope">
            <dict-tag
              :options="dict.type.sys_notice_type"
              :value="scope.row.noticeType"
            />
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.status == '0'">正常</el-tag>
            <el-tag type="danger" v-else>关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建者" align="center" prop="createBy" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        />
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleEdit(scope.row)"
              >修改</el-button
            >
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column></el-table
      >

      <pagination
        v-if="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input
                v-model="form.noticeTitle"
                placeholder="请输入公告标题"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <el-select v-model="form.noticeType" placeholder="请选择公告类型">
                <el-option label="通知" value="1" />
                <el-option label="公告" value="2" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="0">正常</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="noticeContent">
              <editor v-model="form.noticeContent" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { NoticeForm, NoticePageQuery } from "@/api/notice/types";
import { getNoticeList } from "@/api/notice";
import Editor from "@/components/WangEditor/index.vue";
import { getDict } from "@/utils/dict";
import DictTag from "@/components/Dict/DictTag.vue";

const queryFormRef = ref(ElForm);
const queryParams = reactive<NoticePageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const dataList = ref<NoticeForm[]>([]);

const loading = ref(false);

const total = ref(0);

//对话框
const dialog = reactive({
  visible: false,
  title: "",
});

const form = reactive<NoticeForm>({
  noticeTitle: "",
  noticeType: "",
  noticeContent: "",
  status: "",
});

const formRef = ref(ElForm);

const rules = reactive({
  noticeTitle: [
    { required: true, message: "公告标题不能为空", trigger: "blur" },
  ],
  noticeType: [
    { required: true, message: "公告类型不能为空", trigger: "blur" },
  ],
  noticeContent: [
    { required: true, message: "公告内容不能为空", trigger: "blur" },
  ],
  status: [{ required: true, message: "公告状态不能为空", trigger: "blur" }],
});

const handleQuery = () => {
  loading.value = true;
  getNoticeList(queryParams)
    .then(({ data }) => {
      dataList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

const resetQuery = () => {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
};

const handleDelete = (row: NoticeForm | undefined) => {
  ElMessageBox.confirm("确认删除该记录吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {})
    .catch(() => {
      console.log("取消");
    });
};

const handleEdit = (row?: NoticeForm) => {
  dialog.visible = true;
  if (row?.noticeId) {
    dialog.title = "修改公告";

    Object.assign(form, row);
  } else {
    dialog.title = "新增公告";
  }
};

const handleSubmit = () => {};

onMounted(() => {
  handleQuery();
});

//加载
const dict = getDict("sys_notice_type");
</script>
